﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace LP580.CG
{
    /// <summary>
    /// clsGiftProductType 的摘要说明
    /// </summary>
    public class clsGiftProductType
    {
        #region 构造函数

        public clsGiftProductType() { }

        public clsGiftProductType(int prmGiftProductTypeID)
        {
            clsGiftProductType entity = clsGiftProductType.GetByKey(prmGiftProductTypeID);
            this._giftproducttypeid = entity._giftproducttypeid;
            this._userid = entity._userid;
            this._giftproducttypename = entity._giftproducttypename;
            this._giftproducttypeorder = entity._giftproducttypeorder;
        }

        #endregion

        public static clsGiftProductType GetByKey(int prmGiftProductTypeID)
        {
            clsGiftProductType entity = new clsGiftProductType();
            DBase dbo = new DBase();
            System.Data.IDataReader dr = dbo.ExecuteReader("SELECT * FROM [tbl_GiftProductType] WHERE GiftProductTypeID=" + prmGiftProductTypeID.ToString());
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        private static clsGiftProductType DrToClass(System.Data.IDataReader dr)
        {
            clsGiftProductType entity = new clsGiftProductType();
            int intTmp = 0;
            if (!int.TryParse(dr["GiftProductTypeID"].ToString(), out intTmp))
                intTmp = 0;
            entity._giftproducttypeid = intTmp;
            intTmp = 0;
            if (!int.TryParse(dr["UserID"].ToString(), out intTmp))
                intTmp = 0;
            entity._userid = intTmp;
            entity._giftproducttypename = dr["GiftProductTypeName"].ToString();
            intTmp = 0;
            if (!int.TryParse(dr["GiftProductTypeOrder"].ToString(), out intTmp))
                intTmp = 0;
            entity._giftproducttypeorder = intTmp;
            return entity;
        }

        #region 增删改数据库操作

        public static int Insert(clsGiftProductType prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_GiftProductType](" +
                            " [UserID]," +
                            " [GiftProductTypeName]," +
                            " [GiftProductTypeOrder]" +
            ") values (" +
                    "@UserID," +
                    "@GiftProductTypeName," +
                    "@GiftProductTypeOrder" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@GiftProductTypeName", prmEntity._giftproducttypename);
            SaveDTM.AddNewParam("@GiftProductTypeOrder", prmEntity._giftproducttypeorder);
            SaveDTM.Insert();
            return 1;
        }

        public static bool Delete(int prmGiftProductTypeID)
        {
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_GiftProductType] WHERE GiftProductTypeID=" + prmGiftProductTypeID.ToString());
            dbo.CloseConn();
            dbo = null;
            return true;
        }

        public static bool Delete(clsGiftProductType prmEntity)
        {
            return clsGiftProductType.Delete(prmEntity._giftproducttypeid);
        }

        public static bool Update(clsGiftProductType prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = "UPDATE [tbl_GiftProductType] SET " +
                "[UserID] = @UserID, " +
                "[GiftProductTypeName] = @GiftProductTypeName, " +
                "[GiftProductTypeOrder] = @GiftProductTypeOrder " +
            " WHERE GiftProductTypeID=@GiftProductTypeID";
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@GiftProductTypeName", prmEntity._giftproducttypename);
            SaveDTM.AddNewParam("@GiftProductTypeOrder", prmEntity._giftproducttypeorder);
            SaveDTM.AddNewParam("@GiftProductTypeID", prmEntity._giftproducttypeid);
            SaveDTM.Update();
            return true;
        }

        #endregion

        public static List<clsGiftProductType> GetCollection(int prmUserid)
        {
            List<clsGiftProductType> ls = new List<clsGiftProductType>();
            DBase dbo = new DBase();
            string sql = string.Format("SELECT * FROM [tbl_GiftProductType] WHERE UserID={0} ORDER BY GiftProductTypeOrder ASC", prmUserid);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        #region 字段属性

        private int _giftproducttypeid = 0;
        /// <summary>
        /// ID
        /// </summary>
        public int GiftProductTypeID
        {
            get { return _giftproducttypeid; }
            set { _giftproducttypeid = value; }
        }

        private int _userid = 0;
        /// <summary>
        /// 所属用户ID
        /// </summary>
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private string _giftproducttypename = "";
        /// <summary>
        /// 分类名称
        /// </summary>
        public string GiftProductTypeName
        {
            get { return _giftproducttypename; }
            set { _giftproducttypename = value; }
        }

        private int _giftproducttypeorder = 0;
        /// <summary>
        /// 分类排序号
        /// </summary>
        public int GiftProductTypeOrder
        {
            get { return _giftproducttypeorder; }
            set { _giftproducttypeorder = value; }
        }

        #endregion

        public static int GetNextOrderID(int prmUserid)
        {
            int ret = 0;
            DBase dbo = new DBase();
            string sql = string.Format("SELECT TOP 1 GiftProductTypeOrder FROM [tbl_GiftProductType] WHERE UserID={0} ORDER BY GiftProductTypeOrder DESC", prmUserid);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            if (dr.Read())
            {
                if (!int.TryParse(dr[0].ToString(), out ret))
                    ret = 0;
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            ret++;
            return ret;
        }

        public void GoMoveOrderUp()
        {
            clsGiftProductType clsUp = this.getUp();
            if (clsUp.GiftProductTypeID > 0)
            {
                int tmpOrder = this.GiftProductTypeOrder;
                this.GiftProductTypeOrder = clsUp.GiftProductTypeOrder;
                clsUp.GiftProductTypeOrder = tmpOrder;
                clsGiftProductType.Update(this);
                clsGiftProductType.Update(clsUp);
            }
        }

        private clsGiftProductType getUp()
        {
            clsGiftProductType entity = new clsGiftProductType();
            DBase dbo = new DBase();
            string sql = string.Format("SELECT * FROM [tbl_GiftProductType] WHERE UserID={0} AND GiftProductTypeOrder<{1} ORDER BY GiftProductTypeOrder DESC", this.UserID, this.GiftProductTypeOrder);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        public void GoMoveOrderDown()
        {
            clsGiftProductType clsUnder = this.getUnder();
            if (clsUnder.GiftProductTypeID > 0)
            {
                int tmpOrder = this.GiftProductTypeOrder;
                this.GiftProductTypeOrder = clsUnder.GiftProductTypeOrder;
                clsUnder.GiftProductTypeOrder = tmpOrder;
                clsGiftProductType.Update(this);
                clsGiftProductType.Update(clsUnder);
            }
        }

        private clsGiftProductType getUnder()
        {
            clsGiftProductType entity = new clsGiftProductType();
            DBase dbo = new DBase();
            string sql = string.Format("SELECT * FROM [tbl_GiftProductType] WHERE UserID={0} AND GiftProductTypeOrder>{1} ORDER BY GiftProductTypeOrder ASC", this.UserID, this.GiftProductTypeOrder);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        public static List<NameAndCount> GetNameAndCount(int uid)
        {
            List<NameAndCount> ls=new List<NameAndCount>();
            DBase db = new DBase();
            string sql = string.Format("select b.GiftProductTypeName,count(a.GiftProductID) from tbl_GiftProduct as a,tbl_GiftProductType as b where a.GiftProductTypeID=b.GiftProductTypeID and b.UserID={0} group by b.GiftProductTypeName", uid);
            System.Data.IDataReader dr = db.ExecuteReader(sql);
            while (dr.Read())
            {
                NameAndCount nc = new NameAndCount();
                nc.name = dr[0].ToString();
                nc.count = (int)dr[1];
                ls.Add(nc);
            }
            dr.Dispose();
            dr = null;
            db.CloseConn();
            db = null;
            return ls;
        }
    }
    public class NameAndCount
    {
        public string name;
        public int count;
    }
}
